package com.Service.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Dao.BaseDaoI;
import com.Entity.Topic;
import com.Service.TopicService;
@Service(value="topicService")
public class TopicServiceImpl extends BaseServiceImpl<Topic> implements TopicService {

	@Autowired
	private BaseDaoI<Topic> topicDao;

	@Override
	public List<Topic> getTopicListByClubId(int clubCategoryId,int orderflag,int page,int rows) {
		String hql_pre="from Topic t where t.clubCategoryId=:clubCategoryId order by ";
		String hql;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("clubCategoryId", clubCategoryId);
		if(orderflag==0){
			hql=hql_pre+"topicCreatedTime DESC";
		}
		else if(orderflag==1){
			hql=hql_pre+"latestCommentTime DESC";
		}
		else if(orderflag==2){
			hql=hql_pre+"scanNumber DESC";
		}
		else if(orderflag==3){
			hql=hql_pre+"commentNumber DESC";
		}
		else{
			hql=hql_pre+"topicCreatedTime DESC";
		}
		return topicDao.find(hql, params, page, rows);
	}

	@Override
	public long countTopicByClubId(int clubid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("clubCategoryId", clubid);
		String hql="select count(*) from Topic t where t.clubCategoryId=:clubCategoryId";
		return topicDao.count(hql, params);
	}

	@Override
	public long countTopicByTopicClassifyId(int clubid, int topicClassifyId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("clubCategoryId", clubid);
		params.put("topicClassifyId", topicClassifyId);
		String hql="select count(*) from Topic t where t.clubCategoryId=:clubCategoryId and t.topicClassifyId=:topicClassifyId";
		return topicDao.count(hql, params);
	}

	@Override
	public List<Topic> getTopicListByClubIdAndClassifyId(int clubCategoryId,
			int topicClassifyId, int page, int rows) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("clubCategoryId", clubCategoryId);
		params.put("topicClassifyId", topicClassifyId);
		String hql="from Topic t where t.clubCategoryId=:clubCategoryId and t.topicClassifyId=:topicClassifyId order by topicCreatedTime DESC";
		return topicDao.find(hql, params, page, rows);
	}

	@Override
	public List<Topic> getTopicListByTopicClassifyId(int clubid,
			int topicClassifyId, int orderflag, int page, int rows) {
		String hql_pre="from Topic t where t.clubCategoryId=:clubCategoryId and t.topicClassifyId=:topicClassifyId order by ";
		String hql;
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("clubCategoryId", clubid);
		params.put("topicClassifyId", topicClassifyId);
		
		if(orderflag==0){
			hql=hql_pre+"topicCreatedTime DESC";
		}
		else if(orderflag==1){
			hql=hql_pre+"latestCommentTime DESC";
		}
		else if(orderflag==2){
			hql=hql_pre+"scanNumber DESC";
		}
		else if(orderflag==3){
			hql=hql_pre+"commentNumber DESC";
		}
		else{
			hql=hql_pre+"topicCreatedTime DESC";
		}
		
		return topicDao.find(hql, params, page, rows);
	}

	@Override
	public Long countDocTopic(int clubid, int doctorId,
			int topicClassifyId) {
		String hql="select count(*) from Topic t where t.clubCategoryId=:clubCategoryId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("clubCategoryId", clubid);
		if(doctorId>0){
			hql +=" and t.doctorId=:doctorId";
			params.put("doctorId", doctorId);
		}
		if(topicClassifyId>0){
			hql +=" and t.topicClassifyId=:topicClassifyId";
			params.put("topicClassifyId", topicClassifyId);
		}
		return topicDao.count(hql, params);
	}
	
	@Override
	public List<Topic> getDocTopicList(int clubid,
			int doctorId, int topicClassifyId, int orderflag, int currpage,
			int rows) {
		String hql="from Topic t where t.clubCategoryId=:clubCategoryId and t.doctorId=:doctorId";
		if(topicClassifyId == -1){
			hql="from Topic t where  t.doctorId=:doctorId";
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("clubCategoryId", clubid);
		params.put("doctorId", doctorId);
		
		if(topicClassifyId>0){
			params.put("topicClassifyId", topicClassifyId);
			hql +=" and t.topicClassifyId=:topicClassifyId order by ";
		}
		else{
			hql +=" order by ";
		}
		//对排序方式进行判断
		if(orderflag==0){
			hql+="topicCreatedTime DESC";
		}
		else if(orderflag==1){
			hql+="latestCommentTime DESC";
		}
		else if(orderflag==2){
			hql+="scanNumber DESC";
		}
		else if(orderflag==3){
			hql+="commentNumber DESC";
		}
		else{
			hql+="topicCreatedTime DESC";
		}
		return topicDao.find(hql, params, currpage, rows);
	}

}
